package com.hhd.flow.controller;

import com.hhd.flow.vo.CompleteUserTaskVo;
import com.hhd.flow.vo.Result;
import com.hhd.flow.engine.FlowEngine;
import com.hhd.flow.engine.cmd.CommandResult;
import com.hhd.flow.engine.cmd.impl.virtual.VirtualCompleteUserTaskCommand;
import com.hhd.flow.engine.vo.UserManualSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/flow")
public class PreCompleteUserTaskController {
    @Autowired
    FlowEngine flowEngine;

    @Transactional
    @PostMapping("/precomplete")
    public Result complete(@RequestBody CompleteUserTaskVo completeUserTaskVo) {
        VirtualCompleteUserTaskCommand command = flowEngine.newVirtualCompleteUserTaskCommandBuilder()
                .flowUserTaskId(completeUserTaskVo.getFlowUserTaskId())
                .formModel(completeUserTaskVo.getFormModel())
                .build();
        CommandResult<UserManualSelect> result = flowEngine.execute(command);
        return Result.ok(result.getResult());
    }
}
